package com.jisheng.xb.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jisheng.xb.common.StringUtil;
import com.jisheng.xb.dao.ArticleMapper;
import com.jisheng.xb.entity.Article;


/**
 * @description 文章控制器
 * @author LYH
 * @time 2016-7-11 14:49:27
 * */
@Controller
@RequestMapping("article")
public class ArticleController {
	private static final Logger LOG = LoggerFactory.getLogger(ArticleController.class);
	@Autowired
	private ArticleMapper articleMapper;
	
	@RequestMapping(value = {"list"}, method = { RequestMethod.POST,RequestMethod.GET})
	public ModelAndView list(Model model,
			HttpServletRequest request,
			HttpServletResponse response){
		List<Article> list = articleMapper.selectAll();
		ModelAndView mv = new ModelAndView("index").addObject("list", list);
		return mv;
	}
	
	@RequestMapping(value = {"write"}, method = { RequestMethod.POST,RequestMethod.GET})
	public ModelAndView write(Model model,
			HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView mv = new ModelAndView("write");
		return mv;
	}
	
	@RequestMapping(value = {"save"}, method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String save(Article article,
			HttpServletRequest request,
			HttpServletResponse response){
		String type = request.getParameter("type");
		String msg = "保存成功！";
		article.setUpdateTime(new Date());
		article.setAuthor("admin");
		if("publish".equals(type)){
			article.setPublishTime(new Date());
			msg = "发布成功！";
		}
		articleMapper.insert(article);
		
		return JSONObject.toJSONString(msg);
	}
	
	
	@RequestMapping(value = {"update"}, method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String update(Article article,
			HttpServletRequest request,
			HttpServletResponse response){
		String msg = "更新成功！";
		if(article.getId()==null){
			return JSONObject.toJSONString("更新失败");
		}
		articleMapper.updateByPrimaryKeySelective(article);
		return JSONObject.toJSONString(msg);
	}
	
	@RequestMapping(value = {"delete"}, method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String delete(@RequestParam Integer id,
			HttpServletRequest request,
			HttpServletResponse response){
		String msg = "删除成功！";
		articleMapper.deleteByPrimaryKey(id);
		return JSONObject.toJSONString(msg);
	}
	@RequestMapping(value = {"publish"}, method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String publish(Article article,
			HttpServletRequest request,
			HttpServletResponse response){
		String type = request.getParameter("type");
		String content = request.getParameter("content");
		String msg = "保存成功！";
		article.setUpdateTime(new Date());
		article.setCommentcount(0);
		article.setStatus("保存");
		if(article.getId()==null)article.setAuthor("admin");
		if("publish".equals(type)){
			article.setPublishTime(new Date());
			article.setStatus("发布");
			msg = "发布成功！";
		}
		articleMapper.insert(article);
		return JSONObject.toJSONString(msg);
	}
	
	/**
	 * @description 根据文章的标题 、副标题、关键字、作者检索文章
	 * @author LYH
	 * @time 2016-7-11 14:49:27
	 * */
	@RequestMapping(value = {"search"}, method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String search(HttpServletRequest request,HttpServletResponse response){
		//TODO 搜索功能 需要重新加载页面
		String keyWord = request.getParameter("keyWord")==null?"":request.getParameter("keyWord");
		List<Article> list = articleMapper.searchByKeys(keyWord);
		LOG.info(JSONObject.toJSONString(list));
		return  JSONObject.toJSONString(list);
	}
	
	/**
	 * @description 分页获取文章列表
	 * @author LYH
	 * @time 2016-7-11 14:49:27
	 * */
	@RequestMapping(value = {"listByPage"}, method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Model listByPage(Model model,HttpServletRequest request,HttpServletResponse response){
		//TODO 分页查找文章信息
		return model;
	}
	
	/**
	 * @description 获取单个文章的信息
	 * @author LYH
	 * @time 2016-7-11 14:49:27
	 * */
	@RequestMapping(value = {"findById"}, method = { RequestMethod.POST,RequestMethod.GET})
	public ModelAndView findById(Model model,HttpServletRequest request,HttpServletResponse response){
		//TODO 分页查找文章信息
		String id = request.getParameter("id");
		model.addAttribute("msg", "获取成功！");
		model.addAttribute("code","000");
		if(StringUtil.isNullOrEmpty(id)){
			model.addAttribute("msg", "获取失败！参数id 为空！");
		}
		
		return new ModelAndView("articledetail").addObject("obj",articleMapper.selectByPrimaryKey(Integer.valueOf(id)));
	}
	
}
